/**
 * 用户鉴权服务类
 */
package com.xl.web.controller.zoom;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xl.base.annotation.Auth;
import com.xl.base.web.BaseController;
import com.xl.core.util.HtmlUtil;
import com.xl.web.entity.zoom.SysUser;
import com.xl.web.service.zoom.UserService;

@Controller
@RequestMapping("/auth")
public class UserAuthController extends BaseController {
	protected static Logger logger = Logger.getLogger(UserAuthController.class);
	@Autowired(required = false)
	private UserService<SysUser> userService;

	@RequestMapping(value="/loginCheck",method = RequestMethod.POST)
	@ResponseBody
	@Auth(verifyLogin = false) 		
	public void loginCheck(String userAccount,String password)
			throws Exception {
		if(StringUtil.isEmpty(userAccount)||StringUtil.isEmpty(password)){
			this.sendFailureMessage(response, "用户或账号不能为空！");
			return;
		}
		Map<String,String> paraMap = new HashMap<String,String>();
		paraMap.put("userAccount", userAccount);
		paraMap.put("password", password);
	    List<Map> userInfo = userService.queryUserByAccount(paraMap);
	    if(userInfo==null||userInfo.size()==0){
	    	this.sendFailureMessage(response, "用户名或密码错误！");
	    	return;
	    }
	    SysUser user = new SysUser();
	    user.setUserAccount(userAccount);
	    List<SysUser> userList = userService.queryUserInfo(user);
	    user = userList.get(0);
	    //验证通过后将用户信息保存到session中
	    this.request.getSession().setAttribute("userSession", user);
	    Map<String, Object> resultMap = new HashMap<String, Object>();
	    resultMap.put(this.SUCCESS, true);
	    resultMap.put(this.MSG, "验证通过！");
	    resultMap.put("data", userList);
	    HtmlUtil.writerJson(response, resultMap);
	   
	}

	@RequestMapping("/logOut")
	@ResponseBody
	@Auth(verifyLogin = false)
	public void logOut() throws Exception {
		// 返回数据
		try{
		this.request.getSession().removeAttribute("userSession");
		}catch(Exception e){
			logger.info("会话不存在！！！");
		}
		this.sendSuccessMessage(response, "成功退出！");
	}


}
